package com.gy.boot.common.util;

import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
 * 字符串工具类
 * */
public class StrTool {

	public final static String COMMON_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	/**
	 * 生成随机字符串
	 * */
	public static String randomString(int strLength) {
		StringBuilder randomString = new StringBuilder();
		for(int i = 0; i < strLength; i++) {
			int index = (int) Math.abs(COMMON_STRING.length() * Math.random());
			randomString.append(COMMON_STRING.charAt(index));
		}
		return randomString.toString();
	}

	/**
	 * 去掉地址前后/; 如: /test/map/; test/map/; /test/map;  ==> test/map;
	 * */
	public static String removePathSlash(String path) {
		if (BlankUtil.isBlank(path)) {
			return path;
		}
		String slash = "/";
		if (path.startsWith(slash)) {
			path = StrUtil.removePrefix(path, slash);
		}
		if (path.endsWith(slash)) {
			path = StrUtil.removeSuffix(path, slash);
		}
		return path;
	}

	public static String valueOf(Object obj) {
		if (obj == null) {
			return null;
		}
		return String.valueOf(obj).trim();
	}
	public static String valueOfStr(Object obj) {
		if (obj == null) {
			return "";
		}
		return String.valueOf(obj).trim();
	}

	public static String getValue(List<Object> list, Integer index) {
		boolean is_null = index == null || list == null || list.isEmpty() || list.size() <= index;
		String value = is_null ? null : valueOf(list.get(index));
		if (BlankUtil.isBlankNull(value)) {
			return null;
		}
		String errStr = "\"";
		if (value.startsWith(errStr)) {
			value = StrUtil.removeAll(value, errStr);
		}
		if (BlankUtil.isBlank(value)) {
			return null;
		}
		return value;
	}
}
